//
//  ContactsViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/8/30.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ContactsViewController.h"
#import "InputViewController.h"
#import "NSString+Check.h"

@interface ContactsViewController ()<InputViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labelNameOne;
@property (weak, nonatomic) IBOutlet UILabel *labPhoneOne;
@property (weak, nonatomic) IBOutlet UILabel *labNameTwo;
@property (weak, nonatomic) IBOutlet UILabel *labPhoneTwo;

@end

@implementation ContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"紧急联系人";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    NSDictionary *dict = [self.reponst objectForKey:@"result"];
    self.labelNameOne.text = [dict objectForKey:@"emergency_user_name1"];
    self.labPhoneOne.text = [dict objectForKey:@"emergency_user_mobile1"];
    self.labNameTwo.text = [dict objectForKey:@"emergency_user_name2"];
    self.labPhoneTwo.text = [dict objectForKey:@"emergency_user_mobile2"];
}

#pragma mark - 数据请求
- (void)modifyContacts{
    [SVProgressHUD showWithStatus:@"修改紧急联系人..." maskType:SVProgressHUDMaskTypeClear];
    NSDictionary *dic = [self.reponst objectForKey:@"result"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    [dict setObject:[dic objectForKey:@"emergency_user1_id"] forKey:@"emergency_user1_id"];
    [dict setObject:self.labelNameOne.text forKey:@"emergency_user_name1"];
    [dict setObject:self.labPhoneOne.text forKey:@"emergency_user_mobile1"];
    [dict setObject:[dic objectForKey:@"emergency_user2_id"] forKey:@"emergency_user2_id"];
    [dict setObject:self.labNameTwo.text forKey:@"emergency_user_name2"];
    [dict setObject:self.labPhoneTwo.text forKey:@"emergency_user_mobile2"];
    [[BaseDataRequest alloc] modifyEmergencyNmae:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            //[SVProgressHUD showSuccessWithStatus:@"修改紧急联系人成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                NSString *message = [responseObject objectForKey:@"message"];
                if ([message isEqualToString:@""]) {
                    TIPSFORVIEW(@"修改紧急联系人失败");
                }else{
                    TIPSFORVIEW(message);
                }
            }
        }
    }];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    InputViewController *input = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"InputViewController"];
    input.delegate = self;
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            input.inputStyle = InputStyleContactsOne;
        }else{
            input.inputStyle = InputStylePhoneOne;
        }
    }else{
        if (indexPath.row == 0) {
            input.inputStyle = InputStyleContactsTwo;

        }else{
            input.inputStyle = InputStylePhoneTwo;
        }
    }
    [self.navigationController pushViewController:input animated:YES];
}

#pragma mark - InputViewControllerDelegate
- (void)backInputText:(NSString *)text with:(InputStyle)style{
    if (style == InputStyleContactsOne) {
        _labelNameOne.text = text;
    }else if (style == InputStylePhoneOne){
        _labPhoneOne.text = text;
    }else if (style == InputStyleContactsTwo){
        _labNameTwo.text = text;
    }else{
        _labPhoneTwo.text = text;
    }
}

- (IBAction)inputAcion:(id)sender {
        if(![_labelNameOne.text checkUserPassword]){
            TIPSFORVIEW(@"紧急联系人1姓名格式不正确");
            return;
        }
        if (![_labPhoneOne.text checkPhoneNumInput]) {
            TIPSFORVIEW(@"紧急联系人1手机号格式不正确");
            return;
        }
        if(![_labNameTwo.text checkUserPassword]){
            TIPSFORVIEW(@"紧急联系人2姓名格式不正确");
            return;
        }
        if (![_labPhoneTwo.text checkPhoneNumInput]) {
            TIPSFORVIEW(@"紧急联系人2手机号格式不正确");
            return;
        }
    [self modifyContacts];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
